package com.mkvld.appietijdenv2.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import com.mkvld.appietijdenv2.data.Shift.Allowance;
import com.mkvld.appietijdenv2.data.Shift.ShiftType;

public class DayShifts implements Iterable<Shift>, Serializable {
	private static final long serialVersionUID = 5082393537122770311L;
	private List<Shift> shifts;
	private double workTime, sickHours;
	private Allowance allowance;
	
	public DayShifts() {
		shifts = new ArrayList<Shift>();
	}
	
	public List<Shift> getShifts() {
		return shifts;
	}

	public int size() {
		return shifts.size();
	}

	public void add(Shift shift) {
		shifts.add(shift);
	}
	
	public void calculateTimes() {
		workTime = 0;
		allowance = new Allowance();
		for (Shift shift : getMergedShifts()) {
			workTime += shift.getWorkTime();
			allowance.add(shift.getAllowance());
			
			if (shift.getType() == ShiftType.SICK)
				sickHours += shift.getEnd() - shift.getStart();
		}
	}
		
	public List<Shift> getMergedShifts() {
		return shifts;
	}
	
	public double getWorkTime() {
		return workTime;
	}
	
	public double getSickHours() {
		return sickHours;
	}
	
	public Allowance getAllowance() {
		return allowance;
	}

	public void sort() {
		Collections.sort(shifts);
	}

	@Override
	public Iterator<Shift> iterator() {
		return shifts.iterator();
	}
}
